package com.sf.dlb.controller;

import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.service.UserInfoService;
import com.sf.dlb.config.Global;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by jing.tan on 2018/12/10.
 */
@ControllerAdvice
public class BaseController {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    protected Global global;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVO error(Exception e){
        e.printStackTrace();
        return ResultVOUtil.error(ResponseConstant.SERVE_ERROR, "执行出错");
    }
    /**
     * 处理唯一索引的错误
     * @param e
     * @return
     */
    @ExceptionHandler(value=DataIntegrityViolationException.class)
    @ResponseBody
    public Object mySQLIntegrityConstraintViolationException(DataIntegrityViolationException e) {
        logger.info("mySQLIntegrityConstraintViolationException Exception : "+e.getMessage());
        return ResultVOUtil.error(ResponseConstant.ERROR,"数据唯一约束出现重复");
    }
}